package com.lianzai.wxapp.controller;

import com.alibaba.fastjson.JSONObject;
import com.lianzai.common.annotation.NoLoginAuthRequired;
import com.lianzai.common.config.WxConfig;
import com.lianzai.common.constant.Constant;
import com.lianzai.common.utils.R;
import com.lianzai.common.utils.jackson.Json;
import com.lianzai.mobile.domain.PayOrderDto;
import com.lianzai.payment.service.OrderInfoService;
import com.lianzai.user.domain.UserInfoDO;
import com.lianzai.user.service.UserInfoService;
import com.lianzai.wxapp.service.MerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
public class WxPayController {

    @Autowired
    private WxConfig wxConfig;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private Json jsonUtil;

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 用户购买停车月卡，下预订单
     * @param
     * @return
     * @throws Exception
     */
    @NoLoginAuthRequired
    @RequestMapping(value ="/wxapp/pay/commonUserRent", method = RequestMethod.POST)
    @ResponseBody
    public R commonUserRent(@RequestBody JSONObject json) throws Exception{

        UserInfoDO user = userInfoService.getByWxOpenId(json.getString("openId"));

        R r = new R();
        if (user == null)
        {
            r.put("code", 1);
            r.put("msg", "用户未注册");
            return r;
        }

        PayOrderDto payOrderDto = new PayOrderDto();
        payOrderDto.setUserId(json.getLong("userId"));
        payOrderDto.setOpenId(json.getString("openId"));
        payOrderDto.setCarId(json.getLong("carId"));
        payOrderDto.setNums(json.getInteger("productNum"));

        log.info("下预订单 params:{}", jsonUtil.obj2string(payOrderDto));
        R result = orderInfoService.placeXiangparkOrder(payOrderDto);
        log.info("下预订单 result:{}", jsonUtil.obj2string(result));
        return result;


    }


    /**
     * 商户购买停车券，下预订单
     * @param
     * @return
     * @throws Exception
     */
    @NoLoginAuthRequired
    @RequestMapping(value ="/wxapp/pay/merchantBuyTicket", method = RequestMethod.POST)
    @ResponseBody
    public R merchantBuyTicket(@RequestBody JSONObject json) throws Exception{

        UserInfoDO user = userInfoService.getByWxOpenId(json.getString("openId"));

        R r = new R();
        if (user == null)
        {
            r.put("code", 1);
            r.put("msg", "用户未注册");
            return r;
        }

        PayOrderDto payOrderDto = new PayOrderDto();
        payOrderDto.setUserId(json.getLong("userId"));
        payOrderDto.setOpenId(json.getString("openId"));
        payOrderDto.setTransAmt(json.getBigDecimal("fee"));
        payOrderDto.setNums(json.getInteger("ticketNum"));
        payOrderDto.setProductId(json.getLong("ticketId"));

        log.info("下预订单 params:{}", jsonUtil.obj2string(payOrderDto));
        R result = orderInfoService.placeXiangparkTicketOrder(payOrderDto);
        log.info("下预订单 result:{}", jsonUtil.obj2string(result));
        return result;


    }


}
